package com.wing.api.add.company.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wing.api.ApplicationContextUtil;
import com.wing.api.add.company.BaseCompanyService;
import com.wing.cabinet.model.response.EnterpricceIdVO;
import com.wing.common.exception.BusinessException;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.Result;
import com.wing.user.feign.UserClient;
import com.wing.user.model.request.CompanyInfoForm;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 标准岗位接口
 */
@Service
@Slf4j
public class StandardCompanyServiceImpl implements BaseCompanyService {

    @Override
    public void logicProcess(JSONObject jsonObject, EnterpricceIdVO enterpricceIdVo) throws IOException {
        log.info("请求参数,{}", jsonObject.toJSONString());
        //此处需要做接口签名验证
        //sign = md5（参数1+参数2+参数3+key）
        String sourceSign = jsonObject.getString("sign"); // 签名值
        JSONArray data = jsonObject.getJSONArray("data"); //业务数据
        if(StringUtils.isBlank(sourceSign)){
            throw new BusinessException("签名值为空！");
        }
        if(StringUtils.isBlank(data.toJSONString())){
            throw new BusinessException("请求参数为空！");
        }
        /*log.info("签名源串,{}", sourceSign);
        String desSign = DigestUtil.md5Hex("companyName" +"licenseNo"+ enterpricceIdVo.getKey());
        log.info("签名目的串,{}", desSign);
        if(!desSign.equals(sourceSign)){
            throw new BusinessException("签名不通过！");
        }*/

        StringBuffer tempStr = new StringBuffer();
        tempStr.append(data);
        tempStr.append(enterpricceIdVo.getKey());
        log.info("签名源串,{}", sourceSign);
        String desSign = DigestUtil.md5Hex(tempStr.toString());
        log.info("签名目的串,{}", desSign);
        if(!desSign.equals(sourceSign)){
            throw new BusinessException("签名不通过！");
        }
        UserClient userClient = (UserClient) ApplicationContextUtil.getBean("userClientFallback");
        //业务逻辑
        for (int i = 0; i < data.size(); i++) {
            JSONObject jsonObj = data.getJSONObject(i);
            String companyName = jsonObj.getString("companyName");
            if(StringUtils.isEmpty(companyName)){
                throw new BusinessException("第" + (i+1) +  "行企业名称不能为空！");
            }
            JsonResult companyJsonResult = userClient.isOnlyCompanyName(companyName,null);
            if("false".equals(companyJsonResult.getData().toString())){
                throw new BusinessException("第" + (i+1) +"名称必须唯一，该名称：" +companyName+ "已存在！");
            }
            String licenseNo = jsonObj.getString("licenseNo");
            if(StringUtils.isEmpty(licenseNo)){
                throw new BusinessException("第" + (i+1) +  "营业执照号不能为空！");
            }

            //描述
            String remark = jsonObj.getString("remark");
            CompanyInfoForm companyInfoForm = new CompanyInfoForm();
            companyInfoForm.setName(companyName);
            companyInfoForm.setLicenseNo(licenseNo);
            JsonResult jsonResult = userClient.addCompany(companyInfoForm);
            if (jsonResult.getCode() != Result.Code.SUCCESS.getCode()) {
                throw new BusinessException(jsonResult.getMsg());
            }

        }
    }
    public static void main(String[] args) {
//        String desSign = DigestUtil.md5Hex("123456");
//        String desSign2= DigestUtil.md5Hex("companyName" +"licenseNo"+ desSign);
//        System.out.println(desSign);
//        System.out.println(desSign2);

//        String key = "$2a$10$fxpgo/SLFHCeNThnfF/q7uvoZJOxbJBNNI4.dshzlntgBIHKI9eb."; // z0005 测试库的key
        String key = "e10adc3949ba59abbe56e057f20f883e"; // 开发库的key
        JSONObject jsonObject = new JSONObject();
        List<LinkedHashMap<String, Object>> list = new ArrayList<>();
        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
        map.put("companyName", "测试公司1");
        map.put("licenseNo", "ssssssss");
        list.add(map);
        jsonObject.put("data", list);
//        jsonObject.put("data", map);

        StringBuffer tempStr = new StringBuffer();
        tempStr.append(jsonObject.getJSONArray("data"));
        tempStr.append(key);
//        log.info("签名源串,{}", sourceSign);
        String desSign = DigestUtil.md5Hex(tempStr.toString());
//        log.info("签名目的串,{}", desSign);
//        if(!desSign.equals(sourceSign)){
//            throw new BusinessException("签名不通过！");
//        }
        System.out.println(desSign);


    }
}
